#!/bin/bash

function log {
	echo "$1: $0: $2" >&2
}

log INFO "Initializing casperd"

# Loading configuration
source etc/casperd.conf

# System clear
log INFO "Cleaning system..."
# Setting CASPER_RUN_DIR for run
if [ ! -d ${CASPER_RUN_DIR} ]; then
	mkdir ${CASPER_RUN_DIR}
fi
rm -rf ${CASPER_RUN_DIR}/*
if [ -d $CASPER_WORK_DIR ]; then
	rm -Rf $CASPER_WORK_DIR/*
fi

# Setting configuration variables
log INFO "Setting main configuration variables..."
echo variables set CASPER_RUN_DIR ${CASPER_RUN_DIR}
echo variables set CASPER_WORK_DIR ${CASPER_WORK_DIR}
echo variables set CASPER_POOL_DIR ${CASPER_POOL_DIR}

echo variables set NODES_NUMBER ${NODES_NUMBER}
echo variables set NODES_USE ${NODES_USE}

# Loading modules
log INFO "Loading configured modules..."
for mod in $MODULES_LOAD
do
	if [ -f scripts/modules/mod_${MODULES_NAMES[${mod}]}_init.sh ]; then
		scripts/modules/mod_${MODULES_NAMES[${mod}]}_init.sh
	fi
	echo modules load ${MODULES_FILES[$mod]}
done
